#include "hangar.h"

/* Shows the hangar */

// Send in the externs
extern struct cfg_struct *cfg;
extern GLfloat aspect;

extern GLuint cursor_list;
extern int mx, my;

int hangar_main()
{
    // set to 0 to run.  set to non-zero to exit and change gamestate
	int done = 0,i;

    // UI textures
	GLuint hangar_tex[5];
	// Screen positioning (letterbox / pillarbox)
	GLuint xoffset, yoffset;
	float scale_x, scale_y;

	/* Perform a conversion for scaled items to screen coords. */
    float movie_aspect = (1131.0f / 480.0f);
	scale_x = aspect * (float)480.0f;
	scale_y = (float)480.0f;
	xoffset = ((((float)480.0f * aspect) - (float)1131.0f) / 2) + .5;
	yoffset = 0;

	/* Put us into GLOrtho mode */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	/* Match movie resolution */
	/* This is to draw "scaled" UI elements */
	glOrtho(0.0f,scale_x,scale_y,0.0f,-1.0f,1.0f);
    glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	GLuint quad_list = glGenLists(1);
	glNewList(quad_list, GL_COMPILE);
	glBox(xoffset,yoffset,1131.0f,480.0f,0);//frame_tex);
	glEndList();

	/* Load MUSIC */
	Mix_Music *hangar_music = load_music("hangar");
	if(!hangar_music) {
		printf("music_load(\"hangar\"): %s\n", Mix_GetError());
	} else {
		if(Mix_PlayMusic(hangar_music, -1)==-1) {
			printf("Mix_PlayMusic: %s\n", Mix_GetError());
		}
	}

	SDL_Event event;

	unsigned char dirty = 1;

	while (!done)
	{
		/* Check for events */
        /* Keypress skips to video 2 */
		while (SDL_PollEvent (&event))
		{
			switch (event.type)
			{
				case SDL_KEYUP:
					if (event.key.keysym.sym == SDLK_ESCAPE)
						done = GAMESTATE_GAME;
					break;
				case SDL_QUIT:
                    			done = GAMESTATE_EXIT;
					break;
				default:
					break;
			}
		}

		if (dirty) {
			/* Nuke everything */
			glClear(GL_COLOR_BUFFER_BIT);

			glCallList(quad_list);

			glEnable(GL_BLEND);
//			glCallList(hangar_list);
			glDisable(GL_BLEND);

			SDL_GL_SwapBuffers();

			dirty = 0;
		}

		/* Don't run too fast */
		SDL_Delay (1);
	}

	/* shutdown frame timer */
//	SDL_RemoveTimer(frame_timer);

	/* end music */
//	if (hangar_click != NULL) { Mix_FreeChunk(hangar_click); }
	if (hangar_music != NULL) { Mix_HaltMusic(); Mix_FreeMusic(hangar_music); }

    glDeleteLists(quad_list, 1);

//	glDeleteTextures( 5, hangar_tex);

	return done;
}

